/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package RemoteDesktop;

import StaticClass.ArrSocket;
import java.awt.BorderLayout;
import java.awt.Rectangle;
import java.beans.PropertyVetoException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;

/**
 *
 * @author Oho
 */
public class ClientHook extends Thread {

    private JDesktopPane desktop = null;
    private Socket sk = null;
    private JInternalFrame interFrame = new JInternalFrame("Client Screen",
                                                            true, true, true);
    private JPanel cPanel = new JPanel();
    private JFrame frame = new JFrame();
    ClientHook(Socket sk, JDesktopPane desktop) {
        this.sk = sk ;
        this.desktop = desktop;
        ArrSocket.ArrHook .add(this);
    }
      public void drawFrame(){
            frame.add(desktop,BorderLayout.CENTER);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            //Show the frame in a maximized state
            frame.setExtendedState(frame.getExtendedState()|JFrame.MAXIMIZED_BOTH);
            frame.setVisible(true);
    }

    public void drawGUI(){
        interFrame.setLayout(new BorderLayout());
        interFrame.getContentPane().add(cPanel,BorderLayout.CENTER);
        interFrame.setSize(100,100);
        desktop.add(interFrame);
        try {
            //Initially show the internal frame maximized
            interFrame.setMaximum(true);
        } catch (PropertyVetoException ex) {
            ex.printStackTrace();
        }
        //this allows to handle KeyListener events
        cPanel.setFocusable(true);
        interFrame.setVisible(true);
    }

    public ClientHook(Socket sk) {
        this.sk = sk;
    }

    @Override
    public void run() {
        try {
            drawFrame();
            drawGUI();
            
           ObjectInputStream inp = new ObjectInputStream(sk.getInputStream());
          Rectangle  rt = (Rectangle) inp.readObject();
            new ThreadImages(inp,cPanel).start();
            new ClientCommand(sk, cPanel, rt);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ClientHook.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(ClientHook.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
